package rmi.server;

import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import rmi.PolicyFileLocator;
import rmi.Product;
import rmi.WareHouse;

public class Server {

    public static final String CONTEXT_NAME = "java.naming.factory.initial";
    public static final String IIOP_STRING = "com.sun.jndi.cosnaming.CNCtxFactory";
    public static final String URL_NAME = "java.naming.provider.url";
    private String IIOP_URL_STRING;
    private Context context;

    public Server(String address) {
        IIOP_URL_STRING = "iiop://" + address;
        setup(Product.class);
    }

    public static void main(String args[]) {
        new Server(args[0]);
    }

    private void setup(Class someClass) {
        System.setProperty("java.rmi.server.codebase", someClass.getProtectionDomain().getCodeSource().getLocation().toString());
        System.setProperty("java.security.policy", PolicyFileLocator.getLocationOfPolicyFile(true));

        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }

        Properties iiopProperties = new Properties();
        iiopProperties.put(CONTEXT_NAME, IIOP_STRING);
        iiopProperties.put(URL_NAME, IIOP_URL_STRING);
        try {
            context = new InitialContext(iiopProperties);

            try {
                LocateRegistry.createRegistry(1099);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            setupShop(context);
        } catch (NamingException e1) {
            e1.printStackTrace();
        }
    }

    private void setupShop(Context context) {
        try {
            List<Product> productList = new ArrayList<Product>();

            Product p1 = new ProductImpl("Monitor");
            Product p2 = new ProductImpl("Keyboard");
            Product p3 = new ProductImpl("Mouse");

            productList.add(p1);
            productList.add(p2);
            productList.add(p3);

            WareHouse w = new WareHouseImpl(productList);

            PortableRemoteObject.unexportObject(p1);
            PortableRemoteObject.unexportObject(p2);
            PortableRemoteObject.unexportObject(p3);
            PortableRemoteObject.unexportObject(w);

            PortableRemoteObject.exportObject(p1);
            PortableRemoteObject.exportObject(p2);  
            PortableRemoteObject.exportObject(p3);
            PortableRemoteObject.exportObject(w);

            context.rebind("monitor", PortableRemoteObject.toStub(p1));
            context.rebind("keyboard", PortableRemoteObject.toStub(p2));
            context.rebind("mouse", PortableRemoteObject.toStub(p3));
            context.rebind("warehouse", PortableRemoteObject.toStub(w));

            System.out.println("Server online...");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
